import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  {
    path: '/Login',
    name: 'Login',
    component: () => import('../components/Login.vue')
  },
  {
    path: '/Home',
    name: 'Home',
    component: () => import('../components/Home.vue'),
    children: [
      { path: '/Home', redirect: '/Welcome' },
      {
        path: '/Welcome',
        name: 'Welcome',
        component: () => import('../components/Welcome.vue')
      }, {
        path: '/users',
        name: 'users',
        component: () => import('../components/view/users.vue')
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import('../components/power/Rights.vue')
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import('../components/power/Roles.vue')
      },
      /* { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: GoodsList },
      { path: '/goods/add', component: Add },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report } */
    ],
  },
]

const router = new VueRouter({
  routes,
})

//挂载路由守卫
/*  to从哪来
    from到哪去
    next放行函数
    next()放行  next('/Login')强制跳转
    前往Login直接放行
    前往其他页面获取token
*/
router.beforeEach((to, from, next) => {
  if (to.path === '/Login') return next();
  const tokenstr = window.sessionStorage.getItem('token');
  if (!tokenstr) return next('/Login')
  next();
})
export default router
